<?php
use_helper('I18N');
?>
<script type="text/javascript">
    $(function() {
        /*$("#cbo_topupAmount").change(function(){
            var result = parseFloat($(this).val());
            var epointBalance = $('#epointBalance').val();
            $("#convertedAmount").autoNumericSet(result);
            $("#epointBalanceDisplay").autoNumericSet(epointBalance - result);
        });*/

        $("#btnSubmit").click(function(event) {
            event.preventDefault();
            var answer = confirm("<?php echo __('Are you sure you want to reload MT4 Funds?')?>")
            if (answer == true) {
                $("#withdrawForm").submit();
            }
        });
        $("#withdrawForm").validate({
            messages : {
                transactionPassword: {
                    remote: "Security Password is not valid."
                }
            },
            rules : {
                "transactionPassword" : {
                    required : true
                    , remote: "/member/verifyTransactionPassword"
                }
            },
            submitHandler: function(form) {
                waiting();

                if ($("#pt2UserName").val() == "") {
                    alert("MT4 status is pending.");
                    return false;
                }
                var epointBalance = $('#epointBalance').val();
                var pt2Amount = $('#cbo_topupAmount').val();

                if (parseFloat(pt2Amount) <= 0) {
                    alert("MT4 Amount cannot be zero");
                    return false;
                }

                if (parseFloat(pt2Amount) > parseFloat(epointBalance)) {
                    alert("In-sufficient RP");
                    return false;
                }

                form.submit();
            }
        });
        //$("#cbo_topupAmount").trigger("change");
    });
</script>

<td valign="top">

    <?php include_component('component', 'alert', array('param' => $sf_user->getAttribute(Globals::SESSION_DISTID, 0))) ?>

    <form class="form-horizontal label-left" method="post"
          action="/member/reloadTopup"
          data-validate="parsley"
          id="withdrawForm" name="withdrawForm">

        <h2><?php echo __("Reload MT4 Funds"); ?></h2>

        <table cellpadding="5" cellspacing="1">
            <tbody>
            <tr>
                <th>
                    <label class="control-label">
                        <?php echo __("MT4 Account")?>
                    </label>
                </th>
                <td>
                    <select name="pt2UserName" id="pt2UserName" tabindex="1">
<?php
                      if (count($distMt4DBs) >= 1) {
    foreach ($distMt4DBs as $distMt4DB) {
        echo "<option value='" . $distMt4DB->getMt4UserName() . "'>" . $distMt4DB->getMt4UserName() . "</option>";
    }
} else {
    echo "<option value=''>--</option>";
}
    ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th>
                    <label class="control-label">
                        <?php echo __("RP Balance")?>
                    </label>
                </th>
                <td>
                    <input name="epointBalanceDisplay" type="text" id="epointBalanceDisplay" readonly="readonly"
                           value="<?php echo $ledgerBalance; ?>"/>
                </td>
            </tr>
            <tr>
                <th>
                    <label class="control-label">
                        <?php echo __("Reload MT4 Fund")?>
                    </label>
                </th>
                <td>
                    <input name="epointBalance" id="epointBalance" type="hidden" value="<?php echo $ledgerBalance; ?>"/>

                    <select name="pt2Amount" id="cbo_topupAmount" tabindex="2">
<?php
                          for ($i = 100; $i <= 10000; $i = $i + 100) {
    echo "<option value='" . $i . "'>" . number_format($i, 0) . "</option>";
}

    for ($i = 20000; $i <= 50000; $i = $i + 10000) {
        echo "<option value='" . $i . "'>" . number_format($i, 0) . "</option>";
    }
    ?>
                    </select>&nbsp;<?php echo __("USD")?>
                </td>
            </tr>
            <tr>
                <th>
                    <label class="control-label">
                        <?php echo __("Security Password")?>
                    </label>
                </th>
                <td>
                    <input name="transactionPassword" type="password" id="transactionPassword"/>
                </td>
            </tr>

            <tr>
                <td colspan="2">
                    <?php echo __('NOTE :'); ?>
                    <ol class="help-block" style="list-style: decimal; padding-left: 20px;">
                        <li><?php echo __('MT4 Fund Reload will take 2 business days'); ?></li>
                    </ol>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td class="pt10" align="right">
                    <input type="button" id="btnSubmit" class="btn btn-danger" value="<?php echo __("Submit");?>" />
                </td>
            </tr>
            </tbody>
        </table>

    </form>
    <hr/>

    <h2><?php echo __("Reload MT4 Funds Status")?></h2>

    <script type="text/javascript" language="javascript">
        var datagrid = null;
        $(function() {
            datagrid = $("#datagrid").r9jasonDataTable({
                // online1DataTable extra params
                "idTr" : true, // assign <tr id='xxx'> from 1st columns array(aoColumns);
                "extraParam" : function(aoData) { // pass extra params to server
                },
                "reassignEvent" : function() { // extra function for reassignEvent when JSON is back from server
                    reassignDatagridEventAttr();
                },

                // datatables params
                "bLengthChange": true,
                "bFilter": false,
                "bProcessing": true,
                "bServerSide": true,
                "bAutoWidth": false,
                "sAjaxSource": "/finance/reloadMT4FundList",
                "sPaginationType": "full_numbers",
                "aaSorting": [
                    [1,'desc']
                ],
                "aoColumns": [
                    { "sName" : "dist_id", "bVisible" : false,  "bSortable": true},
                    { "sName" : "created_on",  "bSortable": true},
                    { "sName" : "mt4_user_name",  "bSortable": true},
                    { "sName" : "amount",  "bSortable": true},
                    { "sName" : "status_code",  "bSortable": true},
                    { "sName" : "remarks",  "bSortable": true}
                ]
            });
        }); // end function

        function reassignDatagridEventAttr() {
        }

        $(document).ready(function () {
            $('#datagrid thead>tr>th').css('border-bottom', ' 2px rgba(189, 167, 102, 0.4) solid');
        });
    </script>

    <table class="table table-striped" id="datagrid" border="0" width="100%">
        <thead>
        <tr>
            <th></th>
            <th><?php echo __('Date') ?></th>
            <th><?php echo __('MT4 ID') ?></th>
            <th><?php echo __('Amount') ?></th>
            <th><?php echo __('Status') ?></th>
            <th><?php echo __('Remarks') ?></th>
        </tr>
        </thead>
    </table>
    <br/><br/>
</td>
